﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using br.com.SistemaChamados.Controller;
using br.com.SistemaChamados.Enum;
using br.com.SistemaChamados.Model;

public partial class Pages_StateMain : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
    }

    protected void gdvState_RowCommand1(object sender, GridViewCommandEventArgs e)
    {
        int code = Convert.ToInt32(e.CommandArgument);
        switch (e.CommandName)
        {
            case "Exclude":
                excludeState(code);

                break;

            case "Update":
                Session["code"] = code;
                Response.Redirect("StateUpdate.aspx");
                break;
        }
    }

    private void excludeState(int code)
    {
        State state = new State();
        StateController Controller = new StateController();
        state.Code = code;


        try
        {
            if (Controller.DeleteState(state) == EnumGenericState.GENERIC_STATE_SUCCESS)
            {
                Response.Write(@"<script language='javascript'>alert('Estado Excluido com Sucesso!');</script>");
            }
            else
            {
                Response.Write(@"<script language='javascript'>alert('Ocorreu um erro durante a exclusão, por favor tente novamente!');</script>");
            }
        }
        catch (Exception ex)
        {
            Response.Write(@"<script language='javascript'>alert('Ocorreu um erro na exclusão: <br>" + ex.Message +
                               ".');</script>");
            state = null;
        }
        finally
        {
            Response.Redirect("StateMain.aspx");
        }
    }
}